<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['newheader']=1;
if($_RQ['op']=='list'){
	$params=array();
	if($_RQ['plugin_name'] && $_RQ['plugin_name']!='core'){
		$_SESSION['plugin_name']=$_RQ['plugin_name'];
	}
	if($_RQ['plugin_name']=='core'){
		$_SESSION['plugin_name']='';
	}
	if($_SESSION['plugin_name']){
		$params['plugin_name']=$_SESSION['plugin_name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$total='';
	$list=pdo_getslice('core_tpl_template',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$row){
			$row['plugintitle']=pdo_getcolumn('core_plugins',array('name'=>$row['plugin_name']),'title');
		}
	}
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'系统模板消息'),
			array('op'=>'mylist','title'=>'自定义模板消息'),
			array('op'=>'addtpl','title'=>'添加自定义模板'),
		),
		'list'=>array('tip'=>'[温馨提示]：</br>1、每个微信公众号只能<red>添加25个</red>模板，请先检查自己微信公众号模板数是否满了（移步微信公众号官方后台--模板消息，查看），如果满了可能<em>影响新模板消息</em>的推送！
		</br>2、小程序模板消息，是订阅的，每次下单后，会弹窗授权窗口，而且一次只能订阅3条。所以小程序模板消息很多时候无法触发（比如除：支付，发货，确认之外的模板消息都无法触发）；更不会发送订阅消息给管理员/卖家等。因为订阅消息需要点击按钮或者触发支付等行为才会弹窗授权。
		</br>3、小程序行业类目必须包含：IT行业和餐饮行业（因为默认订阅消息模板是属于这两个行业的）
		','psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'total'=>$total,'data'=>'list','list'=>array(
			array('type'=>'string','title'=>'模板标识','name'=>'name'),
			array('type'=>'string','title'=>'模板id','name'=>'template_name'),
			array('type'=>'string','title'=>'小程序订阅id','name'=>'appsubtpl_name'),
			array('type'=>'string','title'=>'模板名称','name'=>'title'),
			array('type'=>'string','title'=>'关联插件','name'=>'plugintitle'),
			array('type'=>'string','title'=>'模板备注（搜索关键词）','name'=>'remark'),
		),'action'=>array(
			array('type'=>'modal','op'=>'demo','title'=>'查看模板'),
			array('type'=>'href','op'=>'addtpl','title'=>'添加自定义模板'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'模板标题','type'=>'string')
		),
	);
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='mylist'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_SESSION['plugin_name']){
		$params['plugin_name']=$_SESSION['plugin_name'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:10;
	$total='';
	$list=pdo_getslice('core_account_tpl_template',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$row){
			$row['plugintitle']=pdo_getcolumn('core_plugins',array('name'=>$row['plugin_name']),'title');
			$row['plugintitle']=$row['plugintitle']?:'系统';
			$row['iswxapp']=$row['wxapp_id']?1:0;
		}
	}
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'系统模板消息'),
			array('op'=>'mylist','title'=>'自定义模板消息'),
			array('op'=>'addtpl','title'=>'添加自定义模板'),
		),
		'list'=>array('psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'total'=>$total,'data'=>'list','list'=>array(
			array('type'=>'string','title'=>'模板标识','name'=>'name'),
			array('type'=>'string','title'=>'模板id','name'=>'template_name'),
			array('type'=>'string','title'=>'模板名称','name'=>'title'),
			array('type'=>'string','title'=>'关联插件','name'=>'plugintitle'),
			array('type'=>'radio','title'=>'类型','name'=>'iswxapp','options'=>'types'),
			array('type'=>'string','title'=>'模板备注（搜索关键词）','name'=>'remark'),
		),'action'=>array(
			array('type'=>'href','op'=>'edittpl','title'=>'设计模板'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>15,'title'=>'模板标题','type'=>'string')
		),
	);
	$return['types']=array('公众号','小程序');
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='demo'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择模板！','error');
	}
	$item=pdo_get('core_tpl_template',array('id'=>$id),array('title','demo','kefumsg'));
	$return['item']=$item;
	$return['htmls']=array(
			'type'=>'look',
			'desc'=>'模板详情',
			'look'=>array(
				array('type'=>'string','title'=>'模板标题','name'=>'title','help'=>''),
				array('type'=>'text','title'=>'微信原型','name'=>'demo','help'=>''),
				array('type'=>'text','title'=>'客服消息','name'=>'kefumsg','help'=>''),
				array('type'=>'text','title'=>'小程序订阅消息原型','name'=>'demoappsub','help'=>''),
			),
	);
	exi($return);
}
if($_RQ['op']=='mydemo'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择模板！','error');
	}
	$item=pdo_get('core_account_tpl_template',array('id'=>$id),array('title','demo','kefumsg'));
	$return['item']=$item;
	$return['htmls']=array(
			'type'=>'look',
			'desc'=>'模板详情',
			'look'=>array(
				array('type'=>'string','title'=>'模板标题','name'=>'title','help'=>''),
				array('type'=>'text','title'=>'微信原型','name'=>'demo','help'=>''),
				array('type'=>'text','title'=>'客服消息','name'=>'kefumsg','help'=>''),
			),
	);
	exi($return);
}
if($_RQ['op']=='addtpl'){
	if($_RQ['id']){	
		$item=pdo_get('core_tpl_template',array('id'=>$_RQ['id']));
		$item['keywords']=json_decode($item['keywords'],true);
	}else{
		$item=array('keywords'=>array(array('title'=>'','value'=>'')));
		//exi('请选择系统模板!','error');
	}
	$item['isfromcore']=1;
	$item['typegroup']=$item['typegroup']?:1;
	if($_RQ['id']){
		$return['msgtpl_tags']=model($item['plugin_name'].'/model')->msgtpl_tags();
	}else{
		$return['msgtpl_tags']=$tags=array(
			array('title'=>'通用类','data'=>array(
				array('name'=>'商城名称'),
				array('name'=>'粉丝昵称'),
			))
		);
	}
	$return['item']=$item;
	$return['messagetype']=array('1'=>'发送模板消息','2'=>'发送客服消息','3'=>'发送混合消息');
	$return['status']=array('1'=>'开启','0'=>'关闭');
	$result = model('core/msg_tpl')->get_wxtpl();
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='edittpl'){
	if($_RQ['id']){	
		$item=pdo_get('core_account_tpl_template',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	}
	$item['keywords']=$item['keywords']?json_decode($item['keywords'],true):array('keywords'=>array(array('title'=>'','value'=>'')));
	$item['typegroup']=$item['typegroup']?:1;
	if($item['plugin_name'] && $item['plugin_name']!='core'){
		$return['msgtpl_tags']=model($item['plugin_name'].'/model')->msgtpl_tags();
	}else{
		$return['msgtpl_tags']=$tags=array(
			array('title'=>'通用类','data'=>array(
				array('name'=>'商城名称'),
				array('name'=>'粉丝昵称'),
			))
		);
	}
	$return['messagetype']=array('1'=>'发送模板消息','2'=>'发送客服消息','3'=>'发送混合消息');
	$return['status']=array('1'=>'开启','0'=>'关闭');
	$result = model('core/msg_tpl')->get_wxtpl();
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['isfromcore']){
		$isaddid=pdo_getcolumn('core_account_tpl_template',array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']),'id');
	}
	$data=array(
				'title'=>$post['title'],
				'template_name'=>$post['template_name'],
				'first'=>$post['first'],
				'firstcolor'=>$post['firstcolor'],
				'keywords'=>ijson_encode($post['keywords']),
				'footer'=>$post['footer'],
				'footercolor'=>$post['footercolor'],
				'remark'=>$post['remark'],
				'kefumsg'=>$post['kefumsg'],
	);
	if($isaddid){
		pdo_update('core_account_tpl_template',$data,array('id'=>$isaddid));
		exi('保存自定义模版成功！','','goback');
	}else{
		$data['name']=$post['name'];
		$data['plugin_name']=$post['plugin_name']?:'core';
		$data['uniacid']=$_SESSION['uniacid'];
		$data['coreid']=intval($post['id']);
		pdo_insert('core_account_tpl_template',$data);
		exi('添加自定义模版成功！','','goback');
	}
	
}
?>